<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('修改房屋类型')"/>

</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">

    <form class="form-horizontal m" id="form-data">

        <!-- 主键隐藏 -->
        <input type="hidden" id="id" name="id" th:field="${houseTypes.id}">

        <div class="form-group">
            <label class="col-sm-3 control-label is-required">名称：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" th:field="${houseTypes.name}" name="name" id="name" required>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label is-required">状态：</label>
            <div class="col-sm-8">
                <select name="status" class="form-control" th:field="${houseTypes.status}"
                        th:with="items=${@dict.getType('sys_normal_disable')}">
                    <option th:each="dict : ${items}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                </select>
            </div>
        </div>

    </form>


</div>


</body>

<th:block th:include="include :: footer"/>

<script type="text/javascript">
    /**
     * 点击了模态框的提交按钮以后触发的事件（名称必须叫 submitHandler）
     */
    function submitHandler() {
        if ($.validate.form()) {
            edit();
        }
    }

    //配置表单验证规则
    $("#form-data").validate({
        onKeyup: false,
        rules: {
            name: {
                remote: {
                    url: ctx + "admin/house/types/exists",
                    dataType: "json",
                    type: "get",
                    data: {
                        "name": function () {
                            return $.common.trim($("#name").val());
                        },
                        "id": function () {
                            return $.common.trim($("#id").val());
                        }
                    },
                    dataFilter: function (data, type) {
                        return $.validate.unique(data);
                    }
                }
            }
        },
        message: {
            name: {
                remote: "该值已被占用",
            }
        },
        focusCleanup: true,
    });

    /**
     * 向后台java发送ajax提交请求
     */
    function edit() {
        //手工收集
        // var formData={};
        // formData.name=$("#form-add input[name='name']").val();
        // formData.status=$("#form-add select[name='status']").val();

        //另一种写法，jquery帮你封装表单序列化方法 (全自动收集)  $.serialize("#form-add")
        var formDataString = $("#form-data").serialize();

        $.ajax({
            type: "post",
            url: ctx + "admin/house/types/edit",
            data: formDataString,
            dataType: "json",
            //同步
            async: false,
            success: function (resp) {
                //调用别人帮你封装好的方法
                $.operate.successCallback(resp);
            }, error: function (e) {
                console.log(e);
                $.modal.alertError("系统错误");
            }
        });

    }

</script>

</html>